package com.contory.ssn_lgnd.app.title;

import com.contory.ssn_lgnd.app.Assets;
import com.contory.ssn_lgnd.app.KeyFrameAnimation;
import com.contory.ssn_lgnd.app.comp.DrawPtr;
import com.contory.ssn_lgnd.app.comp.UpdatePtr;
import com.contory.ssn_lgnd.app.comp.ViewObject;
import com.contory.ssn_lgnd.framework.gl.SpriteBatcher;
import com.contory.ssn_lgnd.framework.gl.TextureRegion;
import com.contory.ssn_lgnd.framework.math.Vector2;

public class TitleLetterObject implements DrawPtr, UpdatePtr{
	TitleLetterObjectChd aquaLetter = new TitleLetterObjectChd();
	TitleLetterBottomChdManager aquaLetterBottom = new TitleLetterBottomChdManager();
	public boolean IsAniEnd = false;
	
	float delayTime = 0.0f;
	static final float delayLimitTime = 0.5f;

	static final byte state_ani = 0;
	static final byte state_end = 1;
	
	byte state_ptr = 0;
	
	public TitleLetterObject(){
		aquaLetter.initialize(10.0f, 5.0f, 0.1f, 0.1f, 1.0f, 1.0f, 1.0f, 1.0f, null, "A Q U A");
	}
	
	@Override
	public void draw(SpriteBatcher batcher) {
		aquaLetter.draw(batcher);
		aquaLetterBottom.draw(batcher);
	}

	@Override
	public void update(float deltaTime) {
		switch(state_ptr){
			case state_ani :
				aquaLetter.update(deltaTime);
				if(aquaLetter.IsAniEnd)
					aquaLetterBottom.update(deltaTime);
				if(aquaLetterBottom.IsAniEnd){
					delayTime += deltaTime;
					if(delayTime > delayLimitTime){
						delayTime = 0.0f;
						state_ptr = state_end;
					}
				}
			break;
			case state_end :
				IsAniEnd = true;
			break;
		}
	}
	
	class TitleLetterBottomChdManager implements UpdatePtr, DrawPtr{
		public boolean IsAniEnd = false;
		TitleLetterBottomChd[] chdArr = new TitleLetterBottomChd[4];

		static final byte state_0Letter = 0;
		static final byte state_1Letter = 1;
		static final byte state_2Letter = 2;
		static final byte state_3Letter = 3;
		static final byte state_end = 4;
		
		byte state_ptr = 0;
		
		float delayTime = 0.0f;
		static final float delayLimitTime = 0.5f;
		
		public TitleLetterBottomChdManager(){
			for(int i = 0; i < chdArr.length; i++){
				chdArr[i] = new TitleLetterBottomChd();
			}
			chdArr[0].initialize(5.5f, 6.5f, 2.0f, 2.0f, 1.0f, 1.0f, 1.0f, 1.0f, null, "W", false);
			chdArr[1].initialize(8.5f, 6.5f, 2.0f, 2.0f, 1.0f, 1.0f, 1.0f, 1.0f, null, "A", false);
			chdArr[2].initialize(11.5f, 6.5f, 2.0f, 2.0f, 1.0f, 1.0f, 1.0f, 1.0f, null, "R", false);
			chdArr[3].initialize(14.5f, 6.5f, 2.0f, 2.0f, 1.0f, 1.0f, 1.0f, 1.0f, null, "Z", false);
		}
		int i = 0;
		@Override
		public void update(float deltaTime) {
			switch(state_ptr){
				case state_0Letter : 
				delayTime += deltaTime;
				if(delayTime > delayLimitTime){
					delayTime = 0.0f;
					chdArr[0].IsDraw = true;
					state_ptr = state_1Letter;
				}
				break;	
				case state_1Letter : 
					delayTime += deltaTime;
					if(delayTime > delayLimitTime){
						delayTime = 0.0f;
						chdArr[1].IsDraw = true;
						state_ptr = state_2Letter;
					}
				break;
				case state_2Letter : 
					delayTime += deltaTime;
					if(delayTime > delayLimitTime){
						delayTime = 0.0f;
						chdArr[2].IsDraw = true;
						state_ptr = state_3Letter;
					}
				break;
				case state_3Letter : 
					delayTime += deltaTime;
					if(delayTime > delayLimitTime){
						delayTime = 0.0f;
						chdArr[3].IsDraw = true;
						state_ptr = state_end;
					}
				break;
				
				case state_end :
					IsAniEnd = true;
				break;
			}
		}

		@Override
		public void draw(SpriteBatcher batcher){
			for(int i = 0; i < chdArr.length; i++){
				if(chdArr[i].IsDraw)
					chdArr[i].draw(batcher);
			}
		}
	}
	
	class TitleLetterBottomChd extends ViewObject implements DrawPtr{
		String text = "";
		public boolean IsDraw = false;
		@Override
		public void draw(SpriteBatcher batcher){
			Assets.font.drawText(batcher, text, x, y, w, h, r, g, b, a);
		}
		
		public void initialize(float x, float y, float w, float h, float r, float g, float b, float a, 
				TextureRegion tr, String text, boolean isDraw){
			this.setXY_WH_TR(x, y, w, h, tr);
			this.setRGBA(r, g, b, a);
			this.text = text;
			this.IsDraw = isDraw;
		}
	}
	
	class TitleLetterObjectChd extends ViewObject implements UpdatePtr, DrawPtr{
		String text = "";
		static final byte state_inc = 0;
		static final byte state_up = 1;
		static final byte state_end = 2;
		
		byte state_ptr = 0;
		
		float limitIncSize = 2.0f;
		float incRate = 0.5f;
		float movEndPosY = 8.5f;
		
		Vector2 accel;
		Vector2 velocity;
		
		public boolean IsAniEnd = false;
		
		public TitleLetterObjectChd(){
			accel = new Vector2(1.2f, 1.2f);
			velocity = new Vector2(0.0f, 0.0f);
		}
		
		@Override
		public void update(float deltaTime) {
			switch(state_ptr){
				case state_inc :
					velocity.add(accel.x * deltaTime, 0);
					if(KeyFrameAnimation.scaleUp(this, deltaTime, velocity.x, limitIncSize)){
						state_ptr = state_up;
						velocity.set(velocity.y, velocity.x);
					}
				break;
				case state_up :
					velocity.add(0, accel.y * deltaTime);
					if(KeyFrameAnimation.translateY(this, deltaTime, velocity.y, movEndPosY)){
						state_ptr = state_end;
						velocity.set(0, 0);
					}
				break;
				
				case state_end:
					IsAniEnd = true;
				break;
			}
		}
		
		@Override
		public void draw(SpriteBatcher batcher){
			Assets.font.drawText(batcher, text, x, y, w, h, r, g, b, a);
		}
		
		public void initialize(float x, float y, float w, float h, float r, float g, float b, float a, 
				TextureRegion tr, String text){
			this.setXY_WH_TR(x, y, w, h, tr);
			this.setRGBA(r, g, b, a);
			this.text = text;
		}
	}
}
